var txt = document.getElementById('txt') //获取文本标签
var chat = document.getElementById('chat')
//输入框键盘事件
function keyupHandle(e){
    if(e.keyCode==13){
        sendMsg(txt.value)
    }
}
/**
 * 向图灵123的api接口发送消息
 * @param {消息内容} msg 
 */
function sendMsg(msg){
    //XMLHttpRequest向远程服务器发送一个请求
    var xhr = new XMLHttpRequest()
    xhr.onreadystatechange = function(){
        if(xhr.readyState==4){
            var res = JSON.parse(xhr.response)
            var strHtml = ''
            if(res.code == 100000){
                strHtml = `<li>${txt.value}</li>
                           <li>${res.text}</li>`
            }
            else if(res.code == 200000){
                strHtml = `<li>${txt.value}</li><li><a target="_blank" href="${res.url}">${res.text}</a></li>`
            }
            else if(res.code == 308000){
                var arrCookBooks = res.list //查询到的数据集合
                var strLists = ''
                arrCookBooks.forEach(function(item){
                    strLists += `<a class="link" href="${item.detailurl}" target="_blank">${item.name}</a>`
                })
                strHtml = `<li>${txt.value}</li><li>${strLists}</li>`
            }
            else{
                strHtml = `<li>${txt.value}</li><li>你说啥？！...</li>`
            }
            txt.value = ''
            chat.innerHTML += strHtml //xhr.response + '<hr>'
        }
    }
    xhr.open('get','http://www.tuling123.com/openapi/api?key=d4e46ce9572d4998823cce88b4861539&info='+msg)
    xhr.send()
}